//
//  MissingDetailsViewController.m
//  Manage
//
//  Created by 杨康 on 2017/11/12.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "MissingDetailsViewController.h"
#import "CarDetailsViewController.h"
#import "CustomAnnotationView.h"
@interface MissingDetailsViewController ()<MAMapViewDelegate>
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) NSMutableArray *annotations;
@end

@implementation MissingDetailsViewController
-(void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:animated];
    // 开启定位
//    self.mapView.showsUserLocation = YES;
//    self.mapView.userTrackingMode = MAUserTrackingModeNone;
    [self initAnnotations];
    
    [self.mapView addAnnotations:self.annotations];
    [self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(100, 50, 100, 50) animated:YES];
}
- (void)initAnnotations
{
    self.annotations = [NSMutableArray array];
    CLLocationCoordinate2D coordinates[2] = {
        {40.004087, 116.348904},
        {39.978234, 116.352792}};
    
    for (int i = 0; i < 2; ++i)
    {
        
        MAPointAnnotation *a1 = [[MAPointAnnotation alloc] init];
        if (i==0) {
            
            a1.title = @"报失位置";
        }else if (i==1)
        {
            
            a1.title = @"丢失位置";
        }
        a1.coordinate = coordinates[i];
        [self.annotations addObject:a1];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"报失详情";
    UIButton *leftButton=(UIButton *)self.navigationItem.leftBarButtonItem.customView;
    leftButton.selected=YES;
    UIButton *rightBtn=[NetworkPath SetNavRightBtnWithController:self withTitle:@"车辆详情" andImageName:nil];
    rightBtn.selected=YES;
    [rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.mapView = [[MAMapView alloc] init];
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(TOP_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-TOP_HEIGHT));
    }];
    
    if ([self.type isEqualToString:@"待审核"]) {
        YKButton *agreeButton=[SingLeton endButton:[UIColor colorQ] andTitle:@"审核通过" RequestSuccess:^(UIButton *btn) {
            
        }];
        [self.mapView addSubview:agreeButton];
        [agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.bottom.mas_equalTo(-15);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-30, 44));
            
        }];
    }
    
    
}
#pragma mark - MAMapViewDelegate
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (updatingLocation)
    {
//        NSLog(@"userlocation :%@", userLocation.location);
    }
}
- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *customReuseIndetifier = @"customReuseIndetifier";
        
        CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];
            // must set to NO, so we can show the custom callout view.
            annotationView.canShowCallout = NO;
            annotationView.draggable = YES;
            annotationView.calloutOffset = CGPointMake(0, -5);
        }
        annotationView.nameLab.text=[NSString stringWithFormat:@"  %@  ",annotation.title];
        if ([annotation.title isEqualToString:@"报失位置"]) {
            
            
            [annotationView.btn3 setImage:[UIImage imageNamed:@"报失位置"] forState:UIControlStateNormal];
        }else if ([annotation.title isEqualToString:@"丢失位置"])
        {
            
            
            [annotationView.btn3 setImage:[UIImage imageNamed:@"丢失位置"] forState:UIControlStateNormal];
        }
        
        
        return annotationView;
    }
    
    return nil;
}
#pragma mark - 车辆详情
-(void)rightAction
{
    CarDetailsViewController *vc=[[CarDetailsViewController alloc]init];
    vc.type=self.type;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
